package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.entity.User;
import com.imooc.exception.BusinessException;
import com.imooc.exception.BusinessExceptionCode;
import com.imooc.mapper.UserMapper;
import com.imooc.service.UserService;
import com.imooc.utils.MD5Utils;
import org.springframework.stereotype.Service;
import sun.plugin2.message.Message;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User checkLogin(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new BusinessException(BusinessExceptionCode.USER_NOT_FOUND);
        }
        String userPassword = user.getPassword();
        if (!userPassword.equals(MD5Utils.md5Digest(password, user.getSalt()))) {
            throw new BusinessException(BusinessExceptionCode.USER_PASSWORD_ERROR);
        }
        return user;
    }
}
